#include<signal.h>
#include<errno.h>
#include<iostream>
#include"Signal.hpp"
#include"Exit.cpp"

bool SignalHandler::flag = false;

bool SignalHandler::getFlag() 
{
	return flag;
}

void SignalHandler::setFlag(bool f) 
{
	flag=f;
}

void SignalHandler::exitSignalHandler(int _ignored)
{
	Hidden::stop();
	History::flush();
	Exit::exit_shell(1);
}

void SignalHandler::hiddenSignalHandler(int _ignored)
{
	Utility::sys_start();
	cerr<<endl<<"Recieved SIGTSTP (Ctrl + Z) signal...Not handled properly"<<endl;
	setFlag(true);
	Utility::sys_end();
}

void SignalHandler::setupSignalHandlers()
{
    if (signal((int) SIGINT, SignalHandler::exitSignalHandler) == SIG_ERR)
    {
        throw SignalException("!!!!! Error setting up signal handlers !!!!!");
    }

    if (signal((int) SIGTSTP, SignalHandler::hiddenSignalHandler) == SIG_ERR)
    {
        throw SignalException("!!!!! Error setting up signal handlers !!!!!");
    }
}
          

